package tree;

/**
 * @ Author     ：侯耀祖
 * @ Description：
 */
public class deleteNode {
    public TreeNode deleteNode(TreeNode root, int key) {
        if (root == null){
            return null;
        }
        root.left = deleteNode(root.left, key);
        root.right = deleteNode(root.right, key);
        if (root.val == key){
            if (root.left == null && root.right == null){
                root = null;
            } else if (root.left != null && root.right == null) {
                root = root.left;
            } else {
                TreeNode node = new TreeNode();
                node = root.right;
                while (node.left != null){
                    node = node.left;
                }
                node.left = root.left;
                root = root.right;
            }
        }
        return root;
    }
}
